import * as Cesium from "cesium";

const unfold = (coordArray: Array<Array<number>>): Array<number>=>{
    const arr: Array<number> = []
    coordArray.forEach((item)=>{
        item.forEach((_item)=>{
            arr.push(_item)
        })
    })

    return arr
}

const draw = (viewer:Cesium.Viewer,coordArray: Array<Array<number>>) =>{
    if(Cesium.defined(viewer.entities.getById("draw"))){
        viewer.entities.removeById("draw")
    };


    if(coordArray.length === 1){
        viewer.entities.add({
            id: "draw",
            position: Cesium.Cartesian3.fromDegreesArrayHeights(unfold(coordArray))[0],
            point: {
                pixelSize: 5,
                color: Cesium.Color.RED
            } 
        })
    }else if(coordArray.length === 2){
        viewer.entities.add({
            id: "draw",
            polyline: {
                positions: Cesium.Cartesian3.fromDegreesArrayHeights(unfold(coordArray)),
                width: 1,
                material: Cesium.Color.RED
            }
        })
    }else if (coordArray.length >= 3){
        viewer.entities.add({
            id: "draw",
            polygon: {
                hierarchy : new Cesium.PolygonHierarchy(Cesium.Cartesian3.fromDegreesArrayHeights(unfold(coordArray))),
                fill: true,
                material:Cesium.Color.RED.withAlpha(0.5),
                outline: true,
                outlineWidth: 3,
                outlineColor: Cesium.Color.RED,
                classificationType: Cesium.ClassificationType.CESIUM_3D_TILE,
            }
        })
    }
}

export default draw;